﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity.Infrastructure;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Gao.Poker.Domain;


namespace Gao.Poker.Services
{
    public class CompetitionService : BaseService
    {
        public static CompetitionService _instance;

        public static CompetitionService Instance()
        {
            return _instance ?? (_instance = new CompetitionService());
        }

        public List<Competition> SearchCompetition()
        {
            return DbContext.Competitions.Where(a=>a.State == 0).ToList();
        }

        /// <summary>
        /// 根据比赛名称查找比赛
        /// </summary>
        /// <param name="competitionName"></param>
        /// <returns></returns>
        public List<Competition> SearchCompetition(String competitionName)
        {
            return DbContext.Competitions.Where(a => a.Name.Contains(competitionName)).ToList();
        }

        public List<Competition> GetAllCompetition(String competitionName,int pageIndex,int pageSize)
        {
            return DbContext.Competitions.AsNoTracking().Where(a => a.Name.Contains(competitionName))
                .OrderBy(a => a.State).Skip(pageIndex * pageSize).Take(pageSize).ToList();
        }

        public int GetAllCompetitionTotal(String competitionName)
        {
            return DbContext.Competitions.Count(a => a.Name.Contains(competitionName));
        }

        public Competition GetCompetition(int id)
        {
            return DbContext.Competitions.Find(id);
        }

        private void Commit()
        {
            DbContext.SaveChanges();
        }

        public Competition Save(Competition competition)
        {
            SaveCompetition(competition);
            Commit();
            return competition;
        }

        public Competition Save(Competition competition, List<Tables> list)
        {
            Save(competition);
            DbContext.Database.ExecuteSqlCommand("DELETE FROM SEATINGS WHERE CompetitionID = " + competition.ID);
           
            foreach (var table in list)
            {
                table.Available = 1;
                DbContext.Tableses.Attach(table);
                DbContext.Entry(table).State = EntityState.Modified;

                int i = 1;
                while (i <= table.Seating)
                {
                    Seating seat = new Seating();
                    seat.CompetitionID = competition.ID;
                    seat.SeatingNum = i;
                    seat.TableNum = table.Number;
                    DbContext.Seatings.Add(seat);
                    i++;
                }                
            }

            Commit();
            return competition;
        }

        private void SaveCompetition(Competition competition)
        {
            if (competition == null) return;

            if (competition.ID == 0)
            {
                DbContext.Competitions.Add(competition);
            }
            else
            {
                var originalCompetition = DbContext.Competitions.FirstOrDefault(a => a.ID == competition.ID);
                var competitionEnttry = DbContext.Entry(originalCompetition);
                competitionEnttry.CurrentValues.SetValues(competition);

                foreach (var level in competition.CompetitionLevels)
                {
                    var originalLevel = DbContext.CompetitionLevels.SingleOrDefault(a => a.ID == level.ID);
                    if (originalLevel != null)
                    {
                        DbContext.Entry(originalLevel).CurrentValues.SetValues(level);
                    }
                    else
                    {
                        level.ID = 0;
                        level.CompetitionID = originalCompetition.ID;
                        DbContext.CompetitionLevels.Add(level);
                    }
                }

                var levels = originalCompetition.CompetitionLevels.Where(a => a.ID != 0).ToList();
                foreach (var competitionLevel in levels)
                {
                    if (competition.CompetitionLevels.All(a => a.ID != competitionLevel.ID))
                    {
                        DbContext.CompetitionLevels.Remove(competitionLevel);
                    }
                }

                foreach (var bouns in competition.Bounses)
                {
                    var originalBouns = DbContext.Bounses.SingleOrDefault(a => a.ID == bouns.ID);
                    if (originalBouns != null)
                    {
                        DbContext.Entry(originalBouns).CurrentValues.SetValues(bouns);
                    }
                    else
                    {
                        bouns.ID = 0;
                        bouns.CompetitionID = originalCompetition.ID;
                        DbContext.Bounses.Add(bouns);
                    }
                }

                var bounses = originalCompetition.Bounses.Where(a => a.ID != 0).ToList();
                foreach (var bs in bounses)
                {
                    if (competition.Bounses.All(a => a.ID != bs.ID))
                    {
                        DbContext.Bounses.Remove(bs);
                    }
                }
            }
        }
    }
}
